package org.dromara.sms4j.core.factory;

import org.dromara.sms4j.aliyun.config.AlibabaConfig;
import org.dromara.sms4j.aliyun.config.MultipleAlibabaSmsConfig;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.cloopen.config.CloopenConfig;
import org.dromara.sms4j.cloopen.config.MultipleCloopenSmsConfig;
import org.dromara.sms4j.comm.enumerate.SupplierType;
import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.dromara.sms4j.emay.config.EmayConfig;
import org.dromara.sms4j.emay.config.MultipleEmaySmsConfig;
import org.dromara.sms4j.huawei.config.HuaweiConfig;
import org.dromara.sms4j.huawei.config.MultipleHuaweiSmsConfig;
import org.dromara.sms4j.jdcloud.config.JdCloudConfig;
import org.dromara.sms4j.jdcloud.config.MultipleJdCloudSmsConfig;
import org.dromara.sms4j.tencent.config.MultipleTencentSmsConfig;
import org.dromara.sms4j.tencent.config.TencentConfig;
import org.dromara.sms4j.unisms.config.MultipleUniSmsConfig;
import org.dromara.sms4j.unisms.config.UniConfig;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 多实例短信工厂
 * 构造工厂，用于获取一个厂商的短信实现对象
 * * 在调用对应厂商的短信发送方法前，请先确保你的配置已经实现，否则无法发送该厂商对应的短信，一般情况下厂商会回执因缺少的配置所造成的的异常，但不会
 * * 以java异常的形式抛出
 *
 * @author easyorder
 * @date 2023/5/23 22:45
 */
public abstract class MultipleSmsFactory {
    private MultipleSmsFactory() {
    }

    public static final Map<String, SupplierType> smsBlendSupplierTypeMap = new ConcurrentHashMap<>();


    /**
     * createSmsBlend
     * <p>获取各个厂商的实现类
     *
     * @param key 厂商枚举
     * @author :Wind
     */
    public static SmsBlend getSmsBlend(String key) {
        SupplierType supplierType = smsBlendSupplierTypeMap.get(key);
        if (supplierType == null) {
            throw new SmsBlendException("Not fount a SmsBlend . Please register first");
        }
        switch (supplierType) {
            case ALIBABA:
                return MultipleAlibabaSmsConfig.getSmsBlend(key);
            case HUAWEI:
                return MultipleHuaweiSmsConfig.getSmsBlend(key);
            case UNI_SMS:
                return MultipleUniSmsConfig.getSmsBlend(key);
            case TENCENT:
                return MultipleTencentSmsConfig.getSmsBlend(key);
            case JD_CLOUD:
                return MultipleJdCloudSmsConfig.getSmsBlend(key);
            case CLOOPEN:
                return MultipleCloopenSmsConfig.getSmsBlend(key);
            case EMAY:
                return MultipleEmaySmsConfig.getSmsBlend(key);
        }
        throw new SmsBlendException("An attempt to construct a SmsBlend object failed. Please check that the enumeration is valid");
    }

    /**
     * 判断是否注册了，如果没有注册则进行注册。
     *
     * @param key
     * @return boolean
     * @throws
     * @author easyorder
     * @date 2023/5/24 21:29
     */
    public static boolean isRegistered(String key) {
        return smsBlendSupplierTypeMap.containsKey(key);
    }


    /**
     * refresh
     * <p>根据厂商类型枚举刷新对应厂商的配置，此方法不会刷新全部厂商的配置对象，只会重构所选厂商的对象，性能损失相对较小
     *
     * @param key    厂商类型枚举
     * @param config 厂商类型枚举
     * @author :Wind
     */
    public static void refresh(String key, Object config) {
        if (config instanceof AlibabaConfig) {
            MultipleAlibabaSmsConfig.refresh(key, (AlibabaConfig) config);
        } else if (config instanceof HuaweiConfig) {
            MultipleHuaweiSmsConfig.refresh(key, (HuaweiConfig) config);
        } else if (config instanceof UniConfig) {
            MultipleUniSmsConfig.refresh(key, (UniConfig) config);
        } else if (config instanceof TencentConfig) {
            MultipleTencentSmsConfig.refresh(key, (TencentConfig) config);
        } else if (config instanceof JdCloudConfig) {
            MultipleJdCloudSmsConfig.refresh(key, (JdCloudConfig) config);
        } else if (config instanceof CloopenConfig) {
            MultipleCloopenSmsConfig.refresh(key, (CloopenConfig) config);
        } else if (config instanceof EmayConfig) {
            MultipleEmaySmsConfig.refresh(key, (EmayConfig) config);
        } else {
            throw new SmsBlendException("An attempt to construct a SmsBlend object failed. Please check that the enumeration is valid");
        }
    }

    /**
     * refresh
     * <p>根据厂商类型枚举刷新对应厂商的配置，此方法不会刷新全部厂商的配置对象，只会重构所选厂商的对象，性能损失相对较小
     *
     * @param config 厂商类型枚举
     * @author :Wind
     */
    public static void register(String key, Object config) {
        if (config instanceof AlibabaConfig) {
            MultipleAlibabaSmsConfig.register(key, (AlibabaConfig) config);
            smsBlendSupplierTypeMap.put(key, SupplierType.ALIBABA);
        } else if (config instanceof HuaweiConfig) {
            MultipleHuaweiSmsConfig.register(key, (HuaweiConfig) config);
            smsBlendSupplierTypeMap.put(key, SupplierType.HUAWEI);
        } else if (config instanceof UniConfig) {
            MultipleUniSmsConfig.register(key, (UniConfig) config);
            smsBlendSupplierTypeMap.put(key, SupplierType.UNI_SMS);
        } else if (config instanceof TencentConfig) {
            MultipleTencentSmsConfig.register(key, (TencentConfig) config);
            smsBlendSupplierTypeMap.put(key, SupplierType.TENCENT);
        } else if (config instanceof JdCloudConfig) {
            MultipleJdCloudSmsConfig.register(key, (JdCloudConfig) config);
            smsBlendSupplierTypeMap.put(key, SupplierType.JD_CLOUD);
        } else if (config instanceof CloopenConfig) {
            MultipleCloopenSmsConfig.register(key, (CloopenConfig) config);
            smsBlendSupplierTypeMap.put(key, SupplierType.CLOOPEN);
        } else if (config instanceof EmayConfig) {
            MultipleEmaySmsConfig.register(key, (EmayConfig) config);
            smsBlendSupplierTypeMap.put(key, SupplierType.EMAY);
        } else {
            throw new SmsBlendException("An attempt to construct a SmsBlend object failed. Please check that the enumeration is valid");
        }

    }

}
